//
//  MainViewController.m
//  TestPhotoMS
//
//  Created by Hung Nguyen Thanh on 10/2/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "MainViewController.h"

#import "NSData+MBBase64.h"

@implementation MainViewController

@synthesize photoTableView;
@synthesize imageView1;
@synthesize imageView2;
@synthesize imageView3;
@synthesize imageView4;
@synthesize imageView5;
@synthesize imageView6;
@synthesize currentImageView;
@synthesize flipsidePopoverController = _flipsidePopoverController;

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    currentImageView = 0;
	// Do any additional setup after loading the view, typically from a nib.
    XMLProcess *xmlProcess = [[XMLProcess alloc] init];
    xmlProcess.XMLDelegate = self;
    [xmlProcess performSelectorInBackground:@selector(parserXMLWithURL:) withObject:GET_PUBLIC_PHOTO];
}

- (void)viewDidUnload
{
    [self setImageView1:nil];
    [self setImageView2:nil];
    [self setImageView3:nil];
    [self setImageView4:nil];
    [self setImageView5:nil];
    [self setImageView6:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    [self parserXMLWithURL:@"http://157.60.199.79/socialPhotosService/WcfOData.svc/Photos"];
    
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    isShow = YES;
    [photoTableView reloadData];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
    isShow = NO;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}

#pragma mark - Flipside View Controller

- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        [self dismissModalViewControllerAnimated:YES];
    } else {
        [self.flipsidePopoverController dismissPopoverAnimated:YES];
    }
}

- (IBAction)showInfo:(id)sender
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideViewController" bundle:nil];
        controller.delegate = self;
        controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentModalViewController:controller animated:YES];
    } else {
        if (!self.flipsidePopoverController) {
            FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideViewController" bundle:nil];
            controller.delegate = self;
            
            self.flipsidePopoverController = [[UIPopoverController alloc] initWithContentViewController:controller];
        }
        if ([self.flipsidePopoverController isPopoverVisible]) {
            [self.flipsidePopoverController dismissPopoverAnimated:YES];
        } else {
            [self.flipsidePopoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        }
    }
}

- (IBAction)login:(id)sender {
    PhotoViewController *photoViewController = [[PhotoViewController alloc] initWithNibName:@"PhotoViewController" bundle:nil];
    photoViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    photoViewController.delegate = self;
    [self presentModalViewController:photoViewController animated:YES];
}

#pragma mark Tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [data count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentifier = @"Cell Identifier";
    UITableViewCell *cell;
    cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    Photo *photo = [data objectAtIndex:indexPath.row];
    
    if (photo.title == nil || [photo.title isEqualToString:@""]) {
        cell.textLabel.text = @"(No title)";
    } else {
        cell.textLabel.text = photo.title;
    }
//    NSLog(@"title: %@", photo.title);
    cell.imageView.image = photo.image;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    Photo *photo = [data objectAtIndex:indexPath.row];
    PhotoViewController *photoViewController = [[PhotoViewController alloc] initWithNibName:@"PhotoViewController" bundle:nil photo:photo];
    photoViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    photoViewController.delegate = self;
    [self presentModalViewController:photoViewController animated:YES];
}

- (void)addCellToTableView{
    if (!isShow) {
        return;
    }
    NSMutableArray *listInsert = [[NSMutableArray alloc] init];
    [listInsert addObject:[NSIndexPath indexPathForRow:[data count] - 1 inSection:0]];
    [photoTableView beginUpdates];
    [photoTableView insertRowsAtIndexPaths:listInsert withRowAnimation:UITableViewRowAnimationRight];
    [photoTableView endUpdates];
}
#pragma mark -

- (void)addImageToImageView:(Photo *)photo{
    UIImageView *imageView = nil;
    switch (currentImageView) {
        case 0:
            imageView = imageView1;
            break;
        case 1:
            imageView = imageView2;
            break;
        case 2:
            imageView = imageView3;
            break;
        case 3:
            imageView = imageView4;
            break;
        case 4:
            imageView = imageView5;
            break;
        case 5:
            imageView = imageView6;
            break;
            
        default:
            currentImageView = 0;
            return;
    }
    imageView.image = photo.thumbImage;
    
    
    
    currentImageView ++;
}

#pragma mark XMLProcess
- (void)haveGetNewImage:(Photo *)newPhoto{
//    [self performSelectorOnMainThread:@selector(addImageToImageView:) withObject:newPhoto waitUntilDone:NO];
    [self addImageToImageView:newPhoto];
}
#pragma mark -


























- (void)photoViewDidEnd:(PhotoViewController *)photoviewController{
    [photoviewController dismissModalViewControllerAnimated:YES];
}

@end
